/*
# @Time    : 2019/6/25 11:21
# @Author  : Mickel Xiang
# @File    : router.go
# @Function: -----------
*/
package main

import (
	"./controllers"
	"./conf"
	"net/http"
	"github.com/coreos/etcd/clientv3"
	"time"
	"fmt"
)

func Router(){

	var config = conf.New("config.json")

	var urls = config.Get("urls").([]interface{})
	var urlsString []string
	for _,v := range urls {
		urlsString = append(urlsString, v.(string))
	}

	// 设置静态目录
	fsh := http.FileServer(http.Dir("./static"))
	http.Handle("/static/", http.StripPrefix("/static/", fsh))

	// 设置 etcd配置
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   urlsString,
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		fmt.Println("connect failed, err:", err)
		return
	}
	etcd:=controllers.NewEtcdController(cli)
	http.HandleFunc("/",etcd.IndexHandler)
	http.HandleFunc("/add",etcd.AddHandler)
	http.HandleFunc("/put",etcd.PutHandler)
	http.HandleFunc("/delete",etcd.DeleteHandler)
}
